package com.crl.ui.bitmaps
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	
	/**
	 *  把一对位图,直接贴在一个sprite 上 ,只用一个sprite 来绘制位图; 并做了缓存操作;
	 * @author crl;
	 * 
	 */	
	public class Grid9BitmapDecoration extends Sprite
	{
		private var bitmapdata:BitmapData;
		private var grid9Rect:Rectangle;
		private var grid9BitmapDatas:Grid9BitmapDatas;
		private var currentRect:Rectangle;
		
		private static var cache:Dictionary
		private static function getCache(bitmapdata:BitmapData):Grid9BitmapDatas{
			if(cache==null){
				cache=new Dictionary(true);
			}else{
				return cache[bitmapdata];
			}
			return null;
		}
		
		public static function removeCache(bitmapdata:BitmapData):Boolean{
			if(cache==null){return false};
			cache[bitmapdata]=null;
			delete cache[bitmapdata];
			return true;
		}
		
		public static function getGrid9BitmapDatas(bitmapdata:BitmapData,rect:Rectangle):Grid9BitmapDatas{
			var result:Grid9BitmapDatas=Grid9BitmapDecoration.getCache(bitmapdata);
			if(result==null){
				
				var dw:int=bitmapdata.width;
				var dh:int=bitmapdata.height;
				
				result=new Grid9BitmapDatas();
				result.bitmaps=new Vector.<BitmapData>();
				result.rectanges=Grid9BitmapDecoration.getGrid9Rectangles(rect,dw,dh);
				result.grid9Rect=rect.clone();
				
				var p:Point=new Point();
				var itemBitmapdata:BitmapData;
				for each(var rect:Rectangle in result.rectanges){
					itemBitmapdata=new BitmapData(rect.width,rect.height,true,0);
					itemBitmapdata.copyPixels(bitmapdata,rect,p);
					result.bitmaps.push(itemBitmapdata);
				}
				
				cache[bitmapdata]=result;
			}
			
			return result;
		}
		
		/**
		 * 
		 * @param rect
		 * @param dw
		 * @param dh
		 * @return 
		 * 
		 */
		public static function getGrid9Rectangles(rect:Rectangle,dw:int,dh:int):Vector.<Rectangle>{
			var rightWidth:int=dw-rect.width-rect.x;
			var rightXpos:int=dw-rect.x;
			var bottomYpos:int=dh-rect.y;
			var bottomHeight:int=dh-rect.height-rect.y;
			
			
			//从左到右, 从上到下;
			var list:Vector.<Rectangle>=new Vector.<Rectangle>();
			list.push(new Rectangle(0,0,rect.x,rect.y));
			list.push(new Rectangle(rect.x,0,rect.width,rect.y) );
			list.push(new Rectangle(rightXpos,0,rightWidth,rect.y) );
			
			list.push(new Rectangle(0,rect.y,rect.x,rect.height) );
			list.push(new Rectangle(rect.x,rect.y,rect.width,rect.height) );
			list.push(new Rectangle(rightXpos,rect.y,rightWidth,rect.height) );
			
			list.push(new Rectangle(0,bottomYpos,rect.x,bottomHeight) );
			list.push(new Rectangle(rect.x,bottomYpos,rect.width,bottomHeight) );
			list.push(new Rectangle(rightXpos,bottomYpos,rightWidth,bottomHeight) );
			
			return list;
			
		}
		
		
		
		
		public function Grid9BitmapDecoration(bitmapdata:BitmapData,grid9Scale:Rectangle=null)
		{
			//this.bitmapdata=bitmapdata;
			this.grid9Rect=grid9Scale;
			
			
			var dw:int=bitmapdata.width;
			var dh:int=bitmapdata.height;
			if(grid9Rect==null){
				var w:int=Math.max(1,dw/4);
				var h:int=Math.max(1,dh/4);
				grid9Rect=new Rectangle(w,h,dw-2*w,dh-2*h);
			}
			
			this.resetBitmapdata(bitmapdata);
			
			/*grid9BitmapDatas=Grid9BitmapDecoration.getGrid9BitmapDatas(bitmapdata,grid9Rect);
			
			//test the cut grid is right;
			//var bit:Bitmap=new Bitmap(bitmapdataList[6]);
			//bit.x=350;
			//this.addChild(bit);
			
			currentRect=grid9BitmapDatas.grid9Rect.clone();
			
			var g:Graphics=this.graphics;
			g.beginBitmapFill(bitmapdata);
			g.drawRect(0,0,dw,dh);
			g.endFill();*/
		}
		
		
		public function resetBitmapdata(bitmapdata:BitmapData):void{
			
			this.bitmapdata=bitmapdata;
			var dw:int=bitmapdata.width;
			var dh:int=bitmapdata.height;
			
			grid9BitmapDatas=Grid9BitmapDecoration.getGrid9BitmapDatas(bitmapdata,grid9Rect);
			currentRect=grid9BitmapDatas.grid9Rect.clone();
			
			var g:Graphics=this.graphics;
			g.beginBitmapFill(bitmapdata);
			g.drawRect(0,0,dw,dh);
			g.endFill();
		}
		
		public function resize(w:int,h:int):void{
			//var startTime:int=getTimer();
			
			var g:Graphics=this.graphics;
			g.clear();
			
			
			//w=Math.max(bitmapdata.width,w);
			//	h=Math.max(bitmapdata.height,h);
			
			var rW:int=bitmapdata.width-grid9Rect.right;
			var bH:int=bitmapdata.height-grid9Rect.bottom;
			
			
			currentRect.width=w-grid9Rect.x-rW;
			currentRect.height=h-grid9Rect.y-bH;
			
			
			
			var list:Vector.<Rectangle>=Grid9BitmapDecoration.getGrid9Rectangles(currentRect,w,h);
			
			var bitmapdataList:Vector.<BitmapData>=grid9BitmapDatas.bitmaps;
			
			var i:int=0;
			var len:int=list.length;
			
			var itemBitmapData:BitmapData;
			var matrix:Matrix=new Matrix;
			var rect:Rectangle;
			
			for(;i<len;i++){
				rect=list[i];
				itemBitmapData=bitmapdataList[i];
				
				//这样写比创建一个新matrix 快1mm;
				matrix.a=matrix.d=1;
				matrix.b=matrix.c=matrix.tx=matrix.ty=0;
				
				if(i==3 || i==4 || i==5){
					matrix.scale(rect.width/itemBitmapData.width,rect.height/itemBitmapData.height);
				}
				matrix.tx=rect.x;
				matrix.ty=rect.y;
				
				//trace(rect);
				
				g.beginBitmapFill(itemBitmapData,matrix);
				
				g.drawRect(rect.x,rect.y,rect.width,rect.height);
			}
			
			//trace("endTime:"+(getTimer()-startTime));
		}
		
		
	}
}
import flash.display.BitmapData;
import flash.geom.Rectangle;


class Grid9BitmapDatas{
	public var rectanges:Vector.<Rectangle>;
	public var bitmaps:Vector.<BitmapData>;
	public var grid9Rect:Rectangle
}